package davion.study.summercloud.controller;

import davion.study.summercloud.model.FileFolder;
import davion.study.summercloud.model.FileStoreStatistics;
import davion.study.summercloud.model.MyFile;
import davion.study.summercloud.model.User;
import davion.study.summercloud.service.FileFolderService;
import davion.study.summercloud.service.MyFileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author: Davion
 * @date: 2020/6/16
 * @description:
 */
@Controller
public class PageController {

    private Logger logger = LoggerFactory.getLogger(PageController.class);

    @Autowired
    MyFileService myFileService;

    @Autowired
    FileFolderService fileFolderService;

    @RequestMapping("/files")
    public String files(HttpServletRequest request,
                        Integer fId, String fName, Integer error, Model model) {
        if (error != null) {
            String msg = null;
            switch (error) {
                case 1:
                    msg = "添加失败！当前已存在同名文件夹";
                    break;
                case 2:
                    msg = "重命名失败！文件夹已存在";
                    break;
                case 3:
                    msg = "服务器出错，操作失败！！！";
                    break;
                case 4:
                    msg = "文件删除失败！！！";
                    break;
                case 5:
                    msg = "文件下载失败！！！";
                    break;
                case 6:
                    msg = "您被禁止操作，请联系管理员。";
                    break;
            }
            model.addAttribute("error", msg);
        }
        User user = (User) request.getSession().getAttribute("user");
        FileStoreStatistics statistics = (FileStoreStatistics) request.getSession().getAttribute("statistics");
        model.addAttribute("user", user);
        model.addAttribute("statistics", statistics);
        List<FileFolder> folders = null;
        List<MyFile> myFiles = null;
        FileFolder nowFolder = null;
        List<FileFolder> location = new ArrayList<>();
        if (fId == null || fId <= 0) {
            // 根目录
            fId = 0;
            folders = fileFolderService.getRootFoldersByUserId(user.getId());
            myFiles = myFileService.getRootFilesByUserId(user.getId());
            nowFolder = new FileFolder();
            nowFolder.setId(fId);
            location.add(nowFolder);
        } else {
            folders = fileFolderService.getFoldersByParentId(fId);
            nowFolder = fileFolderService.getFoldersById(fId);
            myFiles = myFileService.getFilesByFolderId(fId);
            FileFolder temp = nowFolder;
            while (temp.getParentId() != 0) {
                temp = fileFolderService.getFoldersById(temp.getParentId());
                location.add(temp);
            }
        }
        Collections.reverse(location);
        model.addAttribute("folders", folders);
        model.addAttribute("myFiles", myFiles);
        model.addAttribute("nowFolder", nowFolder);
        model.addAttribute("location", location);
        return "u-admin/files";
    }

    @GetMapping("/upload")
    public String toUploadPage(HttpServletRequest request,
            Integer fId, String fName, Model model) {
        User user = (User) request.getSession().getAttribute("user");
        List<FileFolder> folders = null;
        FileFolder nowFolder = null;
        List<FileFolder> location = new ArrayList<>();
        if (fId == null || fId <= 0) {
            fId = 0;
            folders = fileFolderService.getRootFoldersByUserId(user.getId());
            nowFolder = new FileFolder();
            nowFolder.setId(fId);
            location.add(nowFolder);
        } else {
            folders = fileFolderService.getFoldersByParentId(fId);
            nowFolder = fileFolderService.getFoldersById(fId);
            FileFolder temp = nowFolder;
            while (temp.getParentId() != 0) {
                temp = fileFolderService.getFoldersById(temp.getParentId());
                location.add(temp);
            }
        }
        Collections.reverse(location);
        FileStoreStatistics statistics = (FileStoreStatistics) request.getSession().getAttribute("statistics");
        model.addAttribute("user", user);
        model.addAttribute("statistics", statistics);
        model.addAttribute("folders", folders);
        model.addAttribute("nowFolder", nowFolder);
        model.addAttribute("location", location);
        return "u-admin/upload";
    }

    @GetMapping("/doc-files")
    public String toDocFilePage(HttpServletRequest request, Model model){
        User user = (User) request.getSession().getAttribute("user");
        FileStoreStatistics statistics = (FileStoreStatistics) request.getSession().getAttribute("statistics");
        List<MyFile> files = myFileService.getFilesByType(user.getId(), 1);
        model.addAttribute("user", user);
        model.addAttribute("statistics", statistics);
        model.addAttribute("files", files);
        return "u-admin/doc-files";
    }

    @GetMapping("/image-files")
    public String toImageFilePage(HttpServletRequest request, Model model){
        User user = (User) request.getSession().getAttribute("user");
        FileStoreStatistics statistics = (FileStoreStatistics) request.getSession().getAttribute("statistics");
        List<MyFile> files = myFileService.getFilesByType(user.getId(), 2);
        model.addAttribute("user", user);
        model.addAttribute("statistics", statistics);
        model.addAttribute("files", files);
        return "u-admin/image-files";
    }

    @GetMapping("/video-files")
    public String toVideoFilePage(HttpServletRequest request, Model model){
        User user = (User) request.getSession().getAttribute("user");
        FileStoreStatistics statistics = (FileStoreStatistics) request.getSession().getAttribute("statistics");
        List<MyFile> files = myFileService.getFilesByType(user.getId(), 3);
        model.addAttribute("user", user);
        model.addAttribute("statistics", statistics);
        model.addAttribute("files", files);
        return "u-admin/video-files";
    }

    @GetMapping("/music-files")
    public String toMusicFilePage(HttpServletRequest request, Model model){
        User user = (User) request.getSession().getAttribute("user");
        FileStoreStatistics statistics = (FileStoreStatistics) request.getSession().getAttribute("statistics");
        List<MyFile> files = myFileService.getFilesByType(user.getId(), 4);
        model.addAttribute("user", user);
        model.addAttribute("statistics", statistics);
        model.addAttribute("files", files);
        return "u-admin/music-files";
    }

    @GetMapping("/other-files")
    public String toOtherFilepage(HttpServletRequest request, Model model) {
        User user = (User) request.getSession().getAttribute("user");
        FileStoreStatistics statistics = (FileStoreStatistics) request.getSession().getAttribute("statistics");
        List<MyFile> files = myFileService.getFilesByType(user.getId(), 5);
        model.addAttribute("user", user);
        model.addAttribute("statistics", statistics);
        model.addAttribute("files", files);
        return "u-admin/other-files";
    }
}
